﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///AdminUser 的摘要说明
/// </summary>
public partial class AdminUser
{

    public static AdminUser Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminUser.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<AdminUser> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminUser.ToList();
        }
    }
    public static List<AdminUser> GetAllByConditon(int RoleID, string TimeFrom, string TimeTo, string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from admin in context.AdminUser
                        select admin;
            if (RoleID != 0)
                query = query.Where(row => row.RoleID == RoleID);
            if (!string.IsNullOrEmpty(TimeFrom))
                query = query.Where(row => row.LoginTime >= DongDong.Common.Misc.ConvertToDateTime(TimeFrom));
            if (!string.IsNullOrEmpty(TimeTo))
                query = query.Where(row => row.LoginTime <= DongDong.Common.Misc.ConvertToDateTime(TimeTo));
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.UserName.Contains(Key));
            return query.ToList();
        }
    }


    //关联两张表
    public static List<ReturnUserDetail> GetAllByCondito1(int RoleID, string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from admin in context.AdminUser
                        join r in context.AdminRole
                        on admin.RoleID equals r.ID
                        select new ReturnUserDetail
                        {
                            ID = admin.ID,
                            RoleName = r.RoleName
                        };
            if (RoleID != 0)
                query = query.Where(row => row.ID == RoleID);
            //if (!string.IsNullOrEmpty(TimeFrom))
            //    query = query.Where(row => row.LoginTime >= DongDong.Common.Misc.ConvertToDateTime(TimeFrom));
            //if (!string.IsNullOrEmpty(TimeTo))
            //    query = query.Where(row => row.LoginTime <= DongDong.Common.Misc.ConvertToDateTime(TimeTo));
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.RoleName.Contains(Key));
            return query.ToList();
        }
    }

    //左关联
    public static List<ReturnUserDetail> GetAllByConditon2(int RoleID, string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from admin in context.AdminUser
                        join r in context.AdminRole
                        on admin.RoleID equals r.ID
                        into ar
                        from ar1 in ar.DefaultIfEmpty()
                        select new ReturnUserDetail
                        {
                            ID = admin.ID,
                            RoleName = ar1.RoleName ?? "不存在"
                        };
            if (RoleID != 0)
                query = query.Where(row => row.ID == RoleID);
            //if (!string.IsNullOrEmpty(TimeFrom))
            //    query = query.Where(row => row.LoginTime >= DongDong.Common.Misc.ConvertToDateTime(TimeFrom));
            //if (!string.IsNullOrEmpty(TimeTo))
            //    query = query.Where(row => row.LoginTime <= DongDong.Common.Misc.ConvertToDateTime(TimeTo));
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.RoleName.Contains(Key));
            return query.ToList();
        }
    }

    public class ReturnUserDetail
    {
        public int ID { get; set; }
        public string RoleName { get; set; }
    }

    public static List<AdminUser> GetAllByRoleID(int RoleID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminUser.Where(row => row.RoleID == RoleID).ToList();
        }
    }

    public static AdminUser GetAdminUserByUsername_Password(string username, string password)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminUser.FirstOrDefault(row => row.UserName == username && row.Password == password);
        }
    }

    public static AdminUser GetAdminUserByUsername(string username)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AdminUser.FirstOrDefault(row => row.UserName == username);
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.AdminUser.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<AdminUser>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.AdminUser.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.AdminUser.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public void SetCurrent()
    {
        HttpContext.Current.Session["AdminUser"] = this.ID;
    }

    public static AdminUser GetCurrent()
    {

        return AdminUser.Get(DongDong.Common.Misc.ConvertToInt(HttpContext.Current.Session["AdminUser"]));
    }

    public static void ClearCurrent()
    {
        HttpContext.Current.Session["AdminUser"] = null;
    }

}